package com.tange.module.login.google;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.tg.appcommon.android.TGLog;

public class GoogleLoginProxy {

    private static final String TAG = "GoogleLoginProxy";

    public static boolean lastSignedInAccount(Context context) {
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
        return account != null;
    }

    public static void logout(Activity activity, Runnable runnable) {
        GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions
                .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(activity.getString(R.string.tange_third_login_google_client_id))
                .build();

        GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(activity, googleSignInOptions);

        googleSignInClient.signOut().addOnCompleteListener(activity, task -> {
            TGLog.i(TAG, "onComplete ...");
            if (runnable != null) {
                runnable.run();
            }
        });
    }

    public static void launch(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, GoogleLoginActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }
}
